#include <stdio.h>
#include <stdlib.h>
#include "tokens.h"


extern int exp_parse();
extern int exp__scan_string(const char*);

inode*root;

void set_root(void*arg){
    root = (inode*)arg;
}

inode* create_node(int val, void*left, void*right, int op)
{
    inode*new_node = new inode;
    new_node->ival = val;
    new_node->left = (inode*)left;
    new_node->right = (inode*)right;
    new_node->iop = op;
    return new_node;
}

void mid_tra(inode*tmp_ptr)
{
    if(tmp_ptr->iop < 0)printf("%d",tmp_ptr->ival);
    else{
        mid_tra(tmp_ptr->left);
        switch(tmp_ptr->iop)
        {
            case 0:printf("+");break;
            case 1:printf("-");break;
            case 2:printf("*");break;
            case 3:printf("/");break;
            default:printf("other\n");break;
        }
        mid_tra(tmp_ptr->right);
    }
}

char* m_load_string_from_file(char*file_name){
    FILE* fptr = fopen(file_name,"r");
    if(!fptr){
        printf("File open failed in m_load_string_from_file()\n");
       
    }
    int filesize = 0;
    fseek(fptr, 0, SEEK_END);

    filesize = ftell(fptr);
    char*ret = (char*)malloc((filesize+1)*sizeof(char));
    fseek(fptr, 0, SEEK_SET);
    int tmp_val = fread((void*)ret,1,filesize,fptr);
    ret[filesize]='\0';
    fclose(fptr);

    return ret;
}

int main(int argc, char**argv)
{
    if(argc != 2)return 0;

    char* input = m_load_string_from_file(argv[1]);
    exp__scan_string(input);
    exp_parse();
    //mid_tra(root);
    return 0;
}